package com.luhui.codereview.utils

import com.caucho.hessian.io.Hessian2Input
import com.caucho.hessian.io.Hessian2Output
import java.io.*


/**
 * @author luhui
 * @version 1.0
 * @date 2019/11/9 23:52
 */
object SerializableUtils {

    /**
     * 序列化一个对象
     * @param obj
     */
    fun serilalizeObject(obj: Serializable, path: String) {
        val os = FileOutputStream(path)
        os.use { os ->
            val output = Hessian2Output(os)
            try {
                output.writeObject(obj)
            }finally {
                output.close()
            }
        }
    }

    /**
     * 反序列化一个对象
     */
    fun deSerilializeObject(path:String):Serializable{
        val inputStream = FileInputStream(path)
        inputStream.use { inputStream->
            val input = Hessian2Input(inputStream)
            try{
                val obj = input.readObject()
                return obj as Serializable
            }finally {
                input.close()
            }
        }
    }
}
